package com.mvc.security.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mvc.framework.service.PageManager;
import com.mvc.framework.web.PageController;
import com.mvc.security.model.Department;
import com.mvc.security.service.DepartmentManager;
import com.mvc.security.service.UserManager;

@Controller
@RequestMapping("/department")
public class DepartmentController extends PageController<Department> {
	private static final String BASE_DIR = "security/";
	@Autowired
	private DepartmentManager departmentManager;
	@Autowired
	private UserManager userManager;

	@Override
	public PageManager getEntityManager() {
		return departmentManager;
	}

	@Override
	public String getFileBasePath() {
		return BASE_DIR;
	}

	protected String handleEdit(HttpServletRequest request, HttpServletResponse response, Long id) throws Exception {
		List<Department> departments = departmentManager.getAll();
		if(null!=id){
			Object entity = getEntityManager().get(id);
			request.setAttribute("entity", entity);
			departments.remove(entity);
		}
		request.setAttribute("departments", departments);
		request.setAttribute("users", userManager.getAll());
		return BASE_DIR + "editDepartment";
	}
}
